using FactoryAutoMatic.Dao;
using FactoryAutoMatic.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FactoryAutoMatic
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    BasicTool SQl = new BasicTool();

    GetApi Api = new GetApi();

    private void uiButton1_Click(object sender, EventArgs e)
    {

      //2.检查更新

      //3.解压缩 更新最新版本
      //ZipFileDecompression();
      //创建 ini文件
      callConfigIni1();

      //保存配置文件
    }

    public string filePath = @"D:\\Test\\config.ini";
    private void callConfigIni1()
    {
      ConfigIni ini = new ConfigIni();
      //程序名，下面  版本，地址
      ini.INIWrite("section", "key", "val", filePath);
      ini.INIWrite("section", "key", "val2", filePath);

      ini.INIRead("section", "key3", filePath);
      MessageBox.Show(ini.INIRead("section", "key4", filePath));
    }

    public void ZipFileDecompression() {

      string FillName = "result.zip";
      string replace = "111.txt";
      string startPath = "D:\\Test\\txts";// 需要被压缩的文件夹
      string zipPath = @"D:\\Test\\"+ FillName;// 压缩包存储的路径，并为压缩包命名
      string extractPath = @"D:\\Test\\解压缩";// 解压缩文件的存储路径

      //ZipFile.CreateFromDirectory(startPath, zipPath);
     

      if (!Directory.Exists(extractPath)) //若此文件夹不存在
      {
        Directory.CreateDirectory(extractPath); //创建此文件夹
        //Directory.Move(path_A, path_B); //A文件夹剪切成B
        //Directory.Delete(path_B, true); //删除B文件夹 
      }
      else if (File.Exists(@"D:\\Test\\解压缩\\" + replace)) {
        DeleteFile(@"D:\\Test\\解压缩\\" + replace);
      }

      ZipFile.ExtractToDirectory(zipPath, extractPath);

    }


    /// <summary>
    /// 根据路径删除文件
    /// </summary>
    /// <param name="path"></param>

    public void DeleteFile(string path)
    {
      FileAttributes attr = File.GetAttributes(path);
      if (attr == FileAttributes.Directory)
      {
        Directory.Delete(path, true);
      }
      else
      {
        File.Delete(path);
      }
    }
  }
}
